<?php
function getMessageResult($state) {
    if ($state == -1) {
        return "短信尚未开启！";
    } elseif ($state == -2) {
        return "对不起，您没有发送短信权限！";
    } elseif ($state == -3) {
        return "对不起，您超过一天发送次数！";
    } elseif ($state == 0) {
        return "短信发送失败，请联系管理员！";
    }
}

function sendMessagesCode($code, $phones, $uid) {//发送绑定手机短信内容
    $detail = json_decode(getTableField("shortMessage", "three"), true);

    $content = str_replace("{code}", $code, $detail['phone_bind']);
    return sendMobile($phones, $content, 'phone_bind', $uid);
}

function changeOrderContent($id, $field, $mtype = 'email') {//订单内容
    if ($mtype == 'email') {
        $short = json_decode(getTableField("emails", "three"), true);
    } else {
        $short = json_decode(getTableField("shortMessage", "three"), true);
    }
    $content = $short[$field];
    $order = M("order")->field("addtime,sid,ordernum,remark,realMoney,addr,truename,phone,phone2")->where("id = " . $id . "")->find();
    $str_goods = '';
    $sub = M("order_goods")->field("gname,gnum")->where("pid = " . $id . "")->select();
    $num = count($sub);
    foreach ($sub as $k2 => $v2) {
        if ($num != $k2 + 1) {
            $dunhao = "、";
        } else {
            $dunhao = "";
        }
        $str_goods .= $sub[$k2]['gname'] . $sub[$k2]['gnum'] . "份" . $dunhao . "";
    }
    $shop = M("shop")->field("name")->where("id = " . $order['sid'] . "")->find();
    $content = str_replace("{truename}", $order['truename'], $content);
    $content = str_replace("{phone}", $order['phone'], $content);
    $content = str_replace("{phone2}", $order['phone2'], $content);
    $content = str_replace("{addr}", $order['addr'], $content);
    $content = str_replace("{shopname}", $shop['name'], $content);
    $content = str_replace("{addtime}", date("H:i", $order['addtime']), $content);
    $content = str_replace("{content}", $str_goods, $content);
    $content = str_replace("{ordernum}", $order['ordernum'], $content);
    $content = str_replace("{remark}", $order['remark'], $content);
    $content = str_replace("{realMoney}", $order['realMoney'], $content);
    return $content;
}



function sendEmailOrder($orderid) {//发送下单邮箱内容
    $email = json_decode(getTableField("emails", "three"), true);
//  print_r($email);
    if ($email['is_start'] == 1) {
        $order = M("order")->field("sid,uid")->where("id = " . $orderid . "")->find();
        $shop = M("shop")->field("email")->where("id = " . $order['sid'] . "")->find();
        $con = changeOrderContent($orderid, 'send_suc', 'email');
        if ($email['emails']) {
            $emails = explode(",", $email['emails']);
        } else {
            $emails = array();
        }
        if ($shop['email']) {
            array_push($emails, $shop['email']);
        }

        if ($emails) {
            $emails = implode(",", $emails);
            $rs = sendEmail($emails, $con, 'send_suc', $order['uid']);
            if ($rs > 0) {
                M("order")->where("id = " . $orderid . "")->save(array("is_send_email"=>1));
            }
        }
    }
}

function sendEmail($emails, $con, $mtype, $uid) {
    $emailsArr = explode(",", $emails);
    foreach ($emailsArr as $v) {
        $rs = sendMail($v, '', $con, $con);
    }
    if ($emails) {
        $data['email'] = $emails;
        $data['content'] = $con;
        $data['uid'] = $uid;
        $data['state'] = $rs;
        $data['addtime'] = time();
        $data['error'] = $rs;
        $data['num'] = count($emailsArr);
        $data['mtype'] = $mtype;
        $data['ip'] = getIP();
        M("emails")->add($data);
    }
    return $rs;
}
function sendMobileOrder($orderid) {//发送下单邮箱内容
    $three = json_decode(getTableField("shortMessage", "three"), true);
    if ($three['is_start'] == 1) {
        $order = M("order")->field("sid,uid")->where("id = " . $orderid . "")->find();
        $shop = M("shop")->field("phone,is_start_phone")->where("id = " . $order['sid'] . "")->find();
//      echo M("shop")->getlastsql();
        $con = changeOrderContent($orderid, 'send_suc', 'short');
        if ($three['phones']) {
            $phones = explode(",", $three['phones']);
        } else {
            $phones = array();
        }
        if ($shop['phone'] && $shop['is_start_phone'] == 1) {
           array_push($phones, $shop['phone']);
        }
  
        if ($phones) {
            $phones = implode(",", $phones);
           $rs = sendMobile($phones, $con, 'send_suc', $order['uid']);
            if ($rs > 0) {
                M("order")->where("id = " . $orderid . "")->save(array("is_send_phone"=>1));
            }
        }
    }
}
function sendMobile($phones, $con, $mtype, $uid) {
    $detail = json_decode(getTableField("shortMessage", "three"), true);
    if ($detail['is_start'] == 1) {
        if ($mtype == 'send_suc') {
//            if ($detail['users'] == 1) {//是否开启所有用户
//                    $user = M("user")->field("is_shorts")->where("id = " . $uid . "")->find();
//                    if ($user['is_shorts'] == 0) {
//                        return -2;//不是有效用户
//                        exit;
//                    }
//            }
        }

        $limit_times = $detail[$mtype . "_times"];
        if ($limit_times > 0) {
            $today = strtotime(date("Y-m-d 00:00:00"));
            $now = time();
            $num = M("shorts")->where("uid = " . $uid . " AND addtime between " . $today . " AND " . $now . "")->count();
            if ($num > $limit_times) {
                return -3;//超过一天次数
                exit;
            }
        }
        import("Common.Org.mobile");

        $sender = new BayouSmsSender();
        $result = $sender->sendsms($detail['account'], $detail['pwd'], $phones, $con);
//        print_r($result);
        if ($result['status'] == 0) {
            $string = str_replace("gb2312", "utf-8", $result['body']);

            $xml = simplexml_load_string($string);
            $arr = objectToArray($xml->errordescription);
            $error = $arr[0];
        }
        /*         * ****发送短信内容**** */
        $phonesArr = explode(",", $phones);
        if ($phones) {
            $data['phone'] = $phones;
            $data['content'] = $con;
            $data['uid'] = $uid;
            $data['state'] = intval($result['status']);
            $data['addtime'] = time();
            $data['error'] = $error;
            $data['num'] = count($phonesArr);
            $data['mtype'] = $mtype;
            $data['ip'] = getIP();
            M("shorts")->add($data);
        }
        return $result['status'];
    } else {
        return -1;
    }
}

?>
